numbers = [2, 4, 6, 8]
print(numbers)
names = ['Maria', 'Mario', 'Anna', 'Antonio']
print(names)
list
¶names = ['Maria', 'Mario', 'Anna', 'Antonio']
What is the value assigned to names
?
type(names)
number = 1
type(number)
A variable is a reference to a value.
A list
is a value that contains an ordered list of references to values.
NOTES
A list
serves as a collection of values.
What real-world scenarios could you represent with a list?
You can express a list
using square brackets [
and ]
and commas ,
[1, 2, 3, 4]
['dog', 'cat', 'horse']
for ... in ...
¶You can iterate over the values in a list using a for
loop.
numbers = [2, 4, 6, 8]
for number in numbers:
print(number)
for puppy in numbers:
print(puppy)
NOTES
number
updates with each iterationwhile
blocksiteration.py
¶NOTES
fruit
updates with each passfruit
to something else.append(...)
¶append: to put after, to place at the end
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# bigger_numbers = []
for number in numbers:
bigger = number * 2
bigger_numbers.append(bigger)
print(numbers)
print(bigger_numbers)
NOTES
print
be called?append
is a function on the list, just like the Bit
functionsappend
takes one argument. bigger
)smaller_numbers.py
¶This demonstrates the mapping pattern.
Map is a mathematical term that means going from one value to a corresponding value.
This is also referred to as the transform pattern.
NOTES
smaller_numbers
grows with each iterationadd_seven.py
¶You can pass lists to functions and return lists from functions.
NOTES
numbers
is updated to point to the list that new_numbers
points toonly_odds.py
¶This demonstrates the filter pattern.
A new collection is created with certain items filtered out.
NOTES
find_min.py
¶This demonstrates the selection pattern.
A single item is selected from a collection.
NOTES
is None
: this is how you check if a variable points to None
if
conditionsmallest
updates only sometimeslen
¶To get the length of a list
, use len
:
states_visited = ['UT', 'OR', 'WA', 'ID', 'CA',
'NV', 'AZ', 'WY', 'NB', 'SD',
'IA', 'OH', 'NY', 'NJ', 'MA',
'MD', 'VA', 'FL', 'HI', 'PA',
'IL', 'IN', 'MO']
print(len(states_visited))
average.py
¶This demonstrates the accumulator pattern.
total
accumulates the values in the collection.
NOTES
list
for ... in ...:
.append(...)